<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
        #app{
            text-align: center;
        }
    </style>
  </head>
  <body>
    <div id="app">
      <h1>{{msg}}</h1>
      <h2>接受子组件传递过来的值：{{getMsg}}</h2>
        <home title="我是home的标题"></home>

        <home :data='data' @send-to-parent='getData'></home>

        <hr>

        <news /> 
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        let eventBus = new Vue()

        Vue.component('home',{
            props:['title','data'],
            data(){
                return {
                    mymsg:'儿子',
                    getNews:''
                }
            },
            template:
            `
            <div>
            <h2>{{title}}</h2>
            <h3>{{data}}</h3>
            <button @click='sendData'>点击发送成绩给老爸</button>
            <h4 style='color:red'>{{getNews}}</h4>
            </div>
            `,

            methods:{
                sendData(){
                    this.$emit('send-to-parent',this.mymsg)
                }
            },
            mounted() {
                eventBus.$on('to-home',(data)=>{
                    this.getNews=data
                })
            },
        })

        Vue.component("news",{
            template:
            `
            <div>
            <div>new 组件</div>
            <button @click='sendHome'>点击发送到home组件</button>
            </div>
            `,
            methods:{
                sendHome(){
                    eventBus.$emit('to-home','news你要吗')
                }
            }
        })
      var vm = new Vue({
        el: "#app",
        data: {
          msg: "vue",
          data:'我是父亲',
          getMsg:''
        },
        methods: {
            getData(data){
                this.getMsg = data 
            }
        },
      });
    </script>
  </body>
</html>
